package com.dengqi.mymodule.activity.fragment;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.R;
import com.dengqi.mymodule.fragment.NewsFragment;
import com.dengqi.mymodule.fragment.PictureFragment;
import com.dengqi.mymodule.fragment.TypeFragment;
import com.dengqi.mymodule.fragment.VideoFragment;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTabHost;

/**
 * 参考：
 * https://blog.csdn.net/qq_37293612/article/details/54907523
 * https://www.jb51.net/article/120342.htm
 *
 */
public class TabHostActivity extends BaseActivity {

    //定义数组来存放tab的图片选择器
    private int[] mTabImage = {R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher};
    //tab选项卡的文字
    private String[] mTabTitle = {"精华",
            "新帖",
            "是",
            "关注",
            "我的"};
    //每个tab对应的Fragment的字节码对象
    private Class[] fragmentArray = {NewsFragment.class,
            PictureFragment.class,
            VideoFragment.class,
            NewsFragment.class,
            PictureFragment.class};
    //item颜色
    private int[] mTabColor = {R.color.blue,
            R.color.black,
            R.color.colorText2,
            R.color.colorAccent,
            R.color.green};
    //数组 存储Fragment
    private Fragment[] mFragments;
    //当前Fragent的下标
    private int currentIndex=-1;

    private FragmentTabHost tabHost;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_tabhost);

        //获取tabhost
        tabHost = findViewById(android.R.id.tabhost);
        //绑定tabContent
        tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
        //去掉分割线
//        tabHost.getTabWidget().setDividerDrawable(null);
        for (int i = 0; i < fragmentArray.length; i++) {
            //绑定Fragment,添加到的FragmentTabHost
            //设置tab的名称和view
            TabHost.TabSpec tabSpec = tabHost.
                    newTabSpec(mTabTitle[i]).
                    //系统item
//                    setIndicator(mTabTitle[i]);
                    //自定义item
                    setIndicator(getTabItemView(i));
            Bundle bundle = new Bundle();
            bundle.putString("title", mTabTitle[i]);
            //添加tab和关联对应的fragment
            tabHost.addTab(tabSpec, fragmentArray[i], bundle);
            //设置tab的背景色
            tabHost.getTabWidget().
                    getChildAt(i).
                    setBackgroundColor(getResources().getColor(mTabColor[i]));
        }

        mFragments = new Fragment[2];
        for(int i=0;i<2;i++){
            Bundle bundle = new Bundle();
            bundle.putString("title", "type:"+i);
            mFragments[i] = Fragment.instantiate(this, TypeFragment.class.getName(), bundle);
        }

    }

    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(int index) {
        View view = getLayoutInflater().inflate(R.layout.view_bottom, null);
        ImageView imageView = view.findViewById(R.id.iv_tab);
        TextView textView = view.findViewById(R.id.tv_tab);
        //设置图片选择器
        imageView.setImageResource(mTabImage[index]);
        //设置字体选择器
        ColorStateList mColorStateList = getResources().getColorStateList(R.color.selector_nav_black2white);
        textView.setTextColor(mColorStateList);
        //设置tab的文字
        if (TextUtils.isEmpty(mTabTitle[index])) {
            //如果没有名称,则隐藏tab下的textView
            textView.setVisibility(View.GONE);
        } else {
            textView.setVisibility(View.VISIBLE);
            textView.setText(mTabTitle[index]);
        }
        return view;
    }


}
